package org.crc.myvhrb.controller.emp;

import org.crc.myvhrb.model.*;
import org.crc.myvhrb.service.*;
import org.crc.myvhrb.utils.POIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * CRC
 * 2021/8/13
 */
@RestController
@RequestMapping("/employee/basic")
public class EmpBasicController {

    @Autowired
    EmployeeService employeeService;
    @Autowired
    PoliticsStatusService politicsStatusService;
    @Autowired
    NationService nationService;
    @Autowired
    JobLevelService jobLevelService;
    @Autowired
    PositionService positionService;
    @Autowired
    DepartmentService departmentService;


    /**
     * 分页获取员工信息
     */
    @GetMapping("/")
    public RespBeanPage getEmpByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10"
    ) Integer size, Employee employee, Date[] beginDateScope) {
        return employeeService.getEmpByPage(page, size, employee,beginDateScope);
    }

    // 添加员工数据
    @PostMapping("/")
    public RespBean addEmp(@RequestBody Employee employee) {
        if (employeeService.addEmp(employee) == 1) {
            return RespBean.success("添加成功");
        }
        return RespBean.error("添加失败");
    }

    //
    @GetMapping("/politics")
    public List<Politicsstatus> getAllPoliticss() {
        return politicsStatusService.getAllPoliticss();
    }

    @GetMapping("/nation")
    public List<Nation> getAllNations() {
        return nationService.getAllNations();
    }

    // 获取职称列表
    @GetMapping("/joblevel")
    public List<JobLevel> getAllJobLevels() {
        return jobLevelService.getAllJobLevel();
    }

    @GetMapping("/positon")
    public List<Position> getAllPositions() {
        return positionService.getAllPosition();
    }

    @GetMapping("/department")
    public List<Department> getAllDepartments() {
        return departmentService.getAllDepartments();
    }

    // 获取最新工号
    @GetMapping("/maxWorkId")
    public RespBean maxWorkID() {
        RespBean respBean = RespBean.build().setStatus(200)
                .setObj(String.format("%08d", employeeService.maxWorkID() + 1));
        return respBean;
    }

    @DeleteMapping("/{id}")
    public RespBean deleteEmpById(@PathVariable Integer id) {
        if (employeeService.deleteEmpById(id) == 1) {
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }

    @PutMapping("/")
    public RespBean updateEmp(@RequestBody Employee employee) {
        if (employeeService.updateEmp(employee) == 1) {
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }

    // 员工数据下载，转成 Excel 表格
    @GetMapping("/export")
    public ResponseEntity<byte[]> exportData() {
        List<Employee> list = (List<Employee>) employeeService.getEmpByPage(null,null,null,null).getData();
        return POIUtils.employee2Excel(list);
    }

    @PostMapping("/import")
    public RespBean importDaa(MultipartFile file){
        // 将上传的 Excel 数据文件转成 List 数组
        List<Employee> list = POIUtils.excel2Employee(file,nationService.getAllNations(),politicsStatusService.getAllPoliticss(),departmentService.getAllDepartmentsWithOutChildren(),positionService.getAllPosition(),jobLevelService.getAllJobLevel());
        // 将转化好的数据保存到数据库中
        if (employeeService.addEmps(list) == list.size()){
            return RespBean.success("上传成功");
        }
        return RespBean.error("上传失败");
    }


}
